//https://www.coder.work/article/2717278
//https://www.nhooo.com/note/qa5c89.html
//https://stackoverflow.com/questions/58524245/what-is-a-wordarray
//https://www.coder.work/article/1848171
//https://zhuanlan.zhihu.com/p/53411829
var CryptoJS = require("crypto-js");

// Key and random number as arrays, e.g.:
var random_numberBA = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f];
var keyBA = [0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x22, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 0x89, 0x90, 0x02];

// Conversion to WordArrays
var random_numberWA = CryptoJS.enc.Hex.parse(bytesToHex(random_numberBA));
var keyWA = CryptoJS.enc.Hex.parse(bytesToHex(keyBA));

// Encryption
// - random_number as WordArray
// - Key as WordArray
// - ECB-Mode (default: CBC), No-Padding (default: Pkcs7-Padding)
var encryptedCP = CryptoJS.AES.encrypt(random_numberWA, keyWA, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.NoPadding });

// Encrypted data as WordArray
var encryptedWA = encryptedCP.ciphertext;

// Conversion to array
var encryptedBA = hexToBytes(CryptoJS.enc.Hex.stringify(encryptedWA));

// Consume encryptedBA
// ...

// Helper: from https://stackoverflow.com/a/34356351/9014097
function hexToBytes(hex) {
  for (var bytes = [], c = 0; c < hex.length; c += 2)
    bytes.push(parseInt(hex.substr(c, 2), 16));
  return bytes;
}

function bytesToHex(bytes) {
  for (var hex = [], i = 0; i < bytes.length; i++) {
    var current = bytes[i] < 0 ? bytes[i] + 256 : bytes[i];
    hex.push((current >>> 4).toString(16));
    hex.push((current & 0xF).toString(16));
  }
  return hex.join("");
}
let newkey = CryptoJS.enc.Utf8.parse("97KasXdlTzk1ut6y");
let newiv = CryptoJS.enc.Utf8.parse("nCIBpWEcW3GJ7HyB");
// let data = bytesToHex([0x03,0x80,0x5B,0x9F,0x23]);
const data = CryptoJS.lib.WordArray.create([0x03,0x80,0x5B,0x9F,0x23], 5);
console.log(data);

let encryptedData= CryptoJS.AES.encrypt(data, newkey, {iv:newiv, mode: CryptoJS.mode.CFB, padding: CryptoJS.pad.NoPadding });
let wordsArray = encryptedData.ciphertext;
console.log(wordsArray.toString());